package review.generate;

import java.io.File;
import java.io.FileWriter;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;

public class CodeGenerate {

  public static void serviceGenerate(String config, String templateFile, String implFile) throws Exception {
    Map<String, Object> mapConfig = Config.readConfig(config);
    String className = mapConfig.get("entity.class").toString();
    String entityClass = mapConfig.get("package.entity") + "." + className;
    mapConfig.put("className", className);
    mapConfig.put("entityClass", entityClass);
    mapConfig.put("packageDao", mapConfig.get("package.dao"));
    mapConfig = Config.getEntityModel(mapConfig);
    String serivceName = mapConfig.get("serivceName").toString();
    String packageService = mapConfig.get("packageService").toString();
    String url = Config.getBasePath() + "/src/main/java";

    File path = new File(url, packageService.replaceAll("[.]", "/"));
    if (!path.exists()) {
      path.mkdirs();
    }
    String filename = serivceName + ".java";
    File file = new File(path, filename);
    File configPath = new File(Config.getBasePath(), "/target/classes/config");
    Configuration configuration = new Configuration(Configuration.VERSION_2_3_0);
    configuration.setDefaultEncoding("UTF-8");
    configuration.setDirectoryForTemplateLoading(configPath);
    Template tmp = configuration.getTemplate(templateFile);
    FileWriter out = new FileWriter(file);
    tmp.process(mapConfig, out);
    out.close();

    String impl = serivceName + "Impl.java";
    file = new File(path, impl);
    tmp = configuration.getTemplate(implFile);
    out = new FileWriter(file);
    tmp.process(mapConfig, out);
    out.close();
  }

  public static void actionGenerate(String config, String actionTemplate) throws Exception {
    Map<String, Object> mapConfig = Config.readConfig(config);
    String className = mapConfig.get("entity.class").toString();
    String entityClass = mapConfig.get("package.entity") + "." + className;
    mapConfig.put("className", className);
    mapConfig.put("entityClass", entityClass);
    mapConfig.put("packageDao", mapConfig.get("package.dao"));
    mapConfig = Config.getEntityModel(mapConfig);
    String actionClass = mapConfig.get("actionClass").toString();
    String packageAction = mapConfig.get("packageAction").toString();
    String url = Config.getBasePath() + "/src/main/java";

    File path = new File(url, packageAction.replaceAll("[.]", "/"));
    if (!path.exists()) {
      path.mkdirs();
    }
    String filename = actionClass + ".java";
    File file = new File(path, filename);
    File configPath = new File(Config.getBasePath(), "/target/classes/config");
    Configuration configuration = new Configuration(Configuration.VERSION_2_3_0);
    configuration.setDefaultEncoding("UTF-8");
    configuration.setDirectoryForTemplateLoading(configPath);
    Template tmp = configuration.getTemplate(actionTemplate);
    FileWriter out = new FileWriter(file);
    tmp.process(mapConfig, out);
    out.close();
  }

  public static void pageGenerate(String config) throws Exception {
    Map<String, Object> mapConfig = Config.readConfig(config);
    String className = mapConfig.get("entity.class").toString();
    String entityClass = mapConfig.get("package.entity") + "." + className;
    mapConfig.put("className", className);
    mapConfig.put("entityClass", entityClass);
    mapConfig.put("packageDao", mapConfig.get("package.dao"));
    mapConfig = Config.getEntityModel(mapConfig);
    String url = Config.getBasePath() + "/src/main/resources/static/src/views";
    String parent = mapConfig.get("parentURL").toString();
    File path = new File(url, parent);
    if (!path.exists()) {
      path.mkdirs();
    }
    String filename = "index.html";
    File file = new File(path, filename);
    File configPath = new File(Config.getBasePath(), "/target/classes/config");
    Configuration configuration = new Configuration(Configuration.VERSION_2_3_0);
    configuration.setDefaultEncoding("UTF-8");
    configuration.setDirectoryForTemplateLoading(configPath);
    Template tmp = configuration.getTemplate("page.ftl");
    FileWriter out = new FileWriter(file);
    tmp.process(mapConfig, out);
    out.close();

    url = Config.getBasePath() + "/src/main/resources/static/dialog";
    path = new File(url, parent);
    if (!path.exists()) {
      path.mkdirs();
    }
    filename = "addDialog.html";
    file = new File(path, filename);
    tmp = configuration.getTemplate("addDialog.ftl");
    out = new FileWriter(file);
    tmp.process(mapConfig, out);
    out.close();
  }

  public static void main(String[] args) throws Exception {
    // CodeGenerate.serviceGenerate("config.config", "service.ftl", "impl.ftl");
    CodeGenerate.pageGenerate("config.config");
    // actionGenerate("config.config", "action.ftl");
  }

}